﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZXing.QrCode;
using ZXing;
using ZXing.Common;
using ZXing.Rendering;
using System.Drawing.Printing;
using System.IO;
using System.Drawing;
using System.Threading;

namespace zxingTest
{
    public partial class Form1 : Form
    {
        EncodingOptions options = null;
        BarcodeWriter writer = null;

        public Form1()
        {
            InitializeComponent();
            options = new QrCodeEncodingOptions
            {
                DisableECI = true,
                CharacterSet = "UTF-8",
                Width = pictureBox2.Width,
                Height = pictureBox2.Height,
                Margin = 0
            };
            writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options = options;
        }

        /// <summary>
        /// 转换毫米到百分之一英寸
        /// </summary>
        /// <param name="mm"></param>
        /// <returns></returns>
        private int MM2Inch(int mm)
        {
            return (int)(mm * 100.0f / 25.4f);
        }

        PrintDocument printDocument;

        private void pirnt_init()
        {
            printDocument = new PrintDocument();
            //设置打印用的纸张 当设置为Custom的时候，可以自定义纸张的大小，还可以选择A4,A5等常用纸型
            int width = MM2Inch(50);
            int height = MM2Inch(30);
            int margin_lr_in = MM2Inch(0);
            int margin_tb_in = MM2Inch(0);
            printDocument.DefaultPageSettings.PaperSize = new PaperSize("Custum", width, height);
            printDocument.DefaultPageSettings.Margins = new Margins(margin_lr_in, margin_lr_in, margin_tb_in, margin_tb_in);
            printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
            printDocument.PrinterSettings.PrinterName = "ZDesigner GK888t (EPL)";
            //printDocument.PrinterSettings.PrinterName = "Foxit Reader PDF Printer";
        }

        Thread t;

        private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            //e.Graphics.DrawLine(Pens.Black, 0, 0, MM2Inch(10), MM2Inch(10));
            //Point ulCorner = new Point(0, 0);
            
            e.Graphics.DrawImage(pictureBox2.Image, MM2Inch(15), MM2Inch(0), MM2Inch(20), MM2Inch(20));
            e.Graphics.DrawString(IMEI, new Font("宋体", 8, FontStyle.Regular), Brushes.DeepPink, MM2Inch(15), MM2Inch(21));
        }

        private void Send_AT()
        {
            while (true)
            {
                try
                {
                    serial.serialPort1.Write("AT+CGSN=1\r\n");
                }
                catch
                {
                    //MessageBox.Show("请接入模块！");
                }
                IMEI = textBox1.Text;
                if ((IMEI != null)&& (IMEI != ""))
                {
                    Bitmap bitmap = writer.Write(IMEI);
                    pictureBox2.Image = bitmap;
                }
                Thread.Sleep(500);
            }
        }

        public serial serial = new serial();
        string SerialReceivedStr = null;
        bool Send_flag = false;
        string IMEI;
        string IMEI1;

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            serial.serialPort1.DataReceived += serialPort1_DataReceived;
            pirnt_init();
            t = new Thread(Send_AT);
            t.Start();
            //try
            //{
            //    serial.serialPort1.PortName = Properties.Settings.Default.PortName;
            //    serial.serialPort1.BaudRate = Convert.ToInt32(Properties.Settings.Default.BaudRate);
            //    serial.serialPort1.Open();
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}
        }
       
        public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                SerialReceivedStr = serial.serialPort1.ReadTo("OK\r\n");
                //textBox1.AppendText(SerialReceivedStr);

                if (SerialReceivedStr.Contains("+CGSN:"))
                {
                    IMEI = SerialReceivedStr.Substring(SerialReceivedStr.IndexOf("+CGSN:") + 6, 15);
                    textBox1.Text = IMEI;
                    Bitmap bitmap = writer.Write(IMEI);
                    pictureBox2.Image = bitmap;           
                }
            }
            catch
            {
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serial.ShowDialog();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            t.Abort();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //button3.Enabled = false;
            printDocument.Print();
        }
    }
}
